<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $agent app\models\Agent
 * @var $agentPackageList app\models\AgentPackage[] 上级代理商的套餐
 * @var $agentPackageMap app\models\AgentPackage[] 代理商自己的套餐
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '代理商套餐列表';
$this->params['breadcrumbs'][] = '用户管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
<div class="form-group">
</div>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th colspan="6">当前代理商：<?php echo Html::a(Html::encode($agent->username), ['/agent/user/agent-view', 'id' => $agent->id]);?></th>
    </tr>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl"></span>
            </label>
        </th>
        <th>名称</th>
        <th>流量</th>
        <th>成本（月）</th>
        <th>售价（月）</th>
        <th>操作</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($agentPackageList as $agentPackage) {?>
        <tr>
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $agentPackage->id;?>" /><span class="lbl"><?php echo $agentPackage->id;?></span></label></td>
            <td><?php echo Html::encode($agentPackage->package->name);?></td>
            <td><?php echo Yii::$app->formatter->asShortSize($agentPackage->package->data);?></td>
            <td><?php echo $agentPackage->cost_price;?></td>
            <td><?php echo Html::textInput('cost_price[' . $agentPackage->package_id . ']', !isset($agentPackageMap[$agentPackage->package_id]) ? '' : $agentPackageMap[$agentPackage->package_id]->cost_price, ['class' => 'form-control', 'style' => 'width: 80px;']);?></td>
            <td><?php echo ManagerTableOp::widget(['items' => [
                    ['icon' => 'fa fa-check', 'onclick' => 'saveAgentPackage(' . $agent->id . ', ' . $agentPackage->package_id . ')', 'btn_class' => 'btn btn-xs btn-success', 'tip' => '保存', 'color' => 'green'],
                ]]);?></td>
        </tr>
    <?php }?>
    </tbody>
</table>
<script>
    /**
     * 保存代理商套餐价格
     * @param agent_id integer 代理商编号
     * @param package_id integer 套餐编号
     */
    function saveAgentPackage(agent_id, package_id) {
        var costPrice = $('[name="cost_price[' + package_id + ']"]').val();
        $.get('<?php echo Url::to(['/agent/user/save-agent-package']);?>', {agent_id: agent_id, package_id: package_id, cost_price: costPrice}, function (json) {
            if (callback(json)) {
                layer.msg('价格已保存。');
            }
        });
    }
</script>
